package com.app.songbump;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.TextView;

public class SongBumpFriendInfoActivity extends Activity{
	
	Bundle myBundle = new Bundle();
	AppPreferences appPrefs;
	TextView songList;
	TextView friendName;
	String id;
	Boolean shouldStream = true;
	final int SERVERPORT = 6775;
	String SERVERIP= "128.61.20.195";
	PlayStream ps;
	
	private GetSongsReceiver receiver;
	
	public class GetSongsReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			String response = intent.getStringExtra("response");

			ArrayList<Song> songs = new ArrayList<Song>();
			try {
				JSONArray jsonArray = new JSONArray(response);
				for (int index = 0; index < jsonArray.length(); index++) {
					JSONObject jsonObject = jsonArray.getJSONObject(index);
					String artist = jsonObject.getString("artist");
					String title = jsonObject.getString("title");

					songs.add(new Song(artist, title));
				}

			} catch (JSONException e) {
				e.printStackTrace();
			}

			String list = "";
			for (int i = 0; i < songs.size(); i++) {
				list += songs.get(i).toString();
				list += "\n";
			}
			if (songs.size() > 0)
				songList.setText(list);
		}
	}
	
	public class Song {
		
		public String artist;
		public String title;
		
		public Song(String artist,String title){
			this.artist = artist;
			this.title = title;
		}
		
		public String toString(){
			return artist + " - "+title;
		}
		
	}
	
	public void onDestroy(){
		super.onDestroy();
		if (ps.getMediaPlayer() != null){
			ps.getMediaPlayer().pause();
		}
		if(ps != null){
			//ps.stop();
 			ps = null;
 		}
		unregisterReceiver(receiver);
	}
	
	
	 @Override
     public void onCreate(Bundle savedInstanceState) {
	    	
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.friendsongslist);
	        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	        Bundle b = getIntent().getExtras();
	        appPrefs = new AppPreferences(getApplicationContext());
	        
	        friendName = (TextView)this.findViewById(R.id.friendName);
	 		songList = (TextView)this.findViewById(R.id.friendSongs);
	 		friendName.setText(b.getString("name")+"'s Songs");
	 		id = b.getString("id");
	 		
			IntentFilter filter = new IntentFilter(SongBumpServer.GET_SONGS_RESPONSE);
			filter.addCategory(Intent.CATEGORY_DEFAULT);
			receiver = new GetSongsReceiver();
			registerReceiver(receiver, filter);
			
			Intent msgIntent = new Intent(SongBumpFriendInfoActivity.this, SongBumpServer.class);
			msgIntent.putExtra(SongBumpServer.ACTION,SongBumpServer.GET_SONGS);
			msgIntent.putExtra("id",id);
			startService(msgIntent);
	 	
			if(ps != null){
	 			//ps.stop();
	 			ps = null;
	 		}
	 		ps = null;
	 		ps = new PlayStream(b.getString("ip"), SERVERPORT,this.getApplicationContext());
	 		
	 }
	 

}
